package com.sgcc.fj.pmos.e_fj_order.presenter;

import android.text.TextUtils;

import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.fj.pmos.common.base.BasePresenter;
import com.sgcc.fj.pmos.common.bean.BaseResponse;
import com.sgcc.fj.pmos.common.http.HttpInterConst;
import com.sgcc.fj.pmos.common.http.HttpRequestUtils;
import com.sgcc.fj.pmos.common.http.MvpNetCallback;
import com.sgcc.fj.pmos.common.utils.ListPageUtil;
import com.sgcc.fj.pmos.e_fj_order.bean.IndentBean;
import com.sgcc.fj.pmos.e_fj_order.contract.IndentContract;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class IndentPresenter extends BasePresenter<IndentContract.View> implements IndentContract.Presenter {

    @Override
    public void getIndentList(int pageSize, boolean isLoading) {
        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        params.putAll(ListPageUtil.getPageInfo(pageSize));
        params.put("relCode", "");
        params.put("membersSdName", "");
        params.put("startTime", "");
        params.put("endTime", "");
        params.put("flag", "0");//订单传0 合约传1
        HttpRequestUtils.getInstance().request(params, HttpInterConst.ORDER_INDENT_LIST, new MvpNetCallback<IndentBean>(getView(), isLoading) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().queryFailed();
            }

            @Override
            public void onResponse(IndentBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().querySuccess(response);
            }
        });
    }

    @Override
    public void cancelBargain(String relationId) {
        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        params.put("relationId", relationId);
        HttpRequestUtils.getInstance().request(params, HttpInterConst.ORDER_CANCELBARGAIN, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().queryFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                if (response != null) {
                    if (response.isSuccess()) {
                        getView().cancelBargainSuccess(response);
                    } else {
                        if (TextUtils.isEmpty(response.getMsg())) {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                }
            }
        });
    }

    @Override
    public void userRejectBargain(String relationId) {
        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        params.put("relationId", relationId);
        HttpRequestUtils.getInstance().request(params, HttpInterConst.ORDER_USERREJECTBARGAIN, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().queryFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                if (response != null) {
                    if (response.isSuccess()) {
                        getView().userRejectBargainSuccess(response);
                    } else {
                        if (TextUtils.isEmpty(response.getMsg())) {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                }

            }
        });
    }

    @Override
    public void beforeSubmit(String relationId) {
        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        params.put("relationId", relationId);
        HttpRequestUtils.getInstance().request(params, HttpInterConst.ORDER_BEFORESUBMIT, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().queryFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                if (response != null) {
                    if (response.isSuccess()) {
                        getView().userRejectBargainSuccess(response);
                    } else {
                        if (TextUtils.isEmpty(response.getMsg())) {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                }

            }
        });
    }

    @Override
    public void orderCheck(String relationId, String membersId, String combId, String startDate, String endDate) {
        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        params.put("membersId", membersId);
        params.put("combId", combId);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        HttpRequestUtils.getInstance().request(params, HttpInterConst.ORDER_QUOTA_CHECK, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().queryFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                if (response != null) {
                    if (response.isSuccess()) {
                        getView().orderCheckSuccess(relationId);
                    } else {
                        ToastUtils.showShort(response.getMsg());

                    }
                }

            }
        });
    }


}
